import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import router from "./router.js";
import App from "./App.vue";
import MintUi from "mint-ui";
import Vuex from "vuex";
import "../node_modules/mint-ui/lib/style.css";
import "./lib/mui/mui-master/dist/css/mui.css";
import "./lib/mui/mui-master/examples/hello-mui/css/icons-extra.css";

import "./lib/css/index.css";
import VueResource from "vue-resource";
Vue.use(VueResource);
import VuePreview from "vue-preview";
Vue.use(VuePreview);
Vue.use(Vuex);
var car =JSON.parse(localStorage.getItem("car")||"[]");
var store = new Vuex.Store({
    state:{
        car:car,
    },
    mutations:{
        addStateCar(state,goodsInfoCar){
            var flag = false;
            state.car.some(item=>{
                if(item.id==goodsInfoCar.id){
                    item.sellCount += goodsInfoCar.sellCount;
                    flag=true;
                    return true;
                }
            })
            if(!flag){
                state.car.push(goodsInfoCar)
            }
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateGoodsInfo(state,goodsInfoCar){
            state.car.some(item=>{
                if(item.id==goodsInfoCar.id){
                    item.sellCount = parseInt(goodsInfoCar.sellCount)
                    return true;
                }
             })
             localStorage.setItem("car",JSON.stringify(state.car));
        },
        removeCar(state,id){
           state.car.some((item,index)=>{
               if(item.id==id){
                  state.car.splice(index,1);
               }
           })
           localStorage.setItem("car",JSON.stringify(state.car));  
        },
        updateSelect(state,info){
            state.car.some(item=>{
                if(item.id==info.id){
                    item.select=info.select;
                }
            })
          localStorage.setItem("car",JSON.stringify(state.car));  
        }
    },
    getters:{
        getAllCount(state){
            var c = 0;
            state.car.forEach(item=>{
               c+=item.sellCount;
            })
            return c;
        },
        getShopCarCount(state){
           var o = {};
           state.car.forEach(item=>{
               o[item.id]=item.sellCount;
           })
           return o;
        },
        getSelect(state){
            var o = {};
            state.car.forEach(item=>{
                o[item.id] = item.select;
            })
            return o;
        },
        getAllAmount(state){
            var o = {
                allCount:0,
                allPrice:0
            };
            state.car.forEach(item=>{
                if(item.select){
                    o.allCount+=item.sellCount;
                    o.allPrice += item.price*item.sellCount;
                }
            });
            return o;
            
        }
    }
})
Vue.http.options.root = "http://www.liulongbin.top:3005";
Vue.http.options.emulateJSON = true;
Vue.use(MintUi);
import moment from "moment";
Vue.filter("dateFormat",function(dateString,pattern="YYYY-MM-DD HH:mm:ss"){
   return  moment(dateString).format(pattern);
})
new Vue({
    el:"#app",
    render:function(create){
        return create(App);
     },
     router,
     store
})